package com.gxa.zhonglong.service.impl;

import com.gxa.zhonglong.common.ExceptionCode;
import com.gxa.zhonglong.exception.BusinessException;
import com.gxa.zhonglong.mapper.VipcardMapper;
import com.gxa.zhonglong.pojo.Vipcard;
import com.gxa.zhonglong.service.VipcardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @auther JJuventus
 * @date 2021/10/26 15:00
 */
@Service("VipcardService")
public class VipcardServiceImpl implements VipcardService {

    @Autowired
    private VipcardMapper vipcardMapper = null;


    @Override
    public List<Vipcard> select(Integer page, Integer size) {
        Integer offside = (page-1)*size;
        List<Vipcard> vipcards = vipcardMapper.select(offside, size);
        return vipcards;
    }

    @Override
    public List<Vipcard> selectAll() {
        return vipcardMapper.selectAll();
    }

    @Override
    public Integer add(Vipcard vipcard) throws BusinessException {
        Vipcard vipcard1 = vipcardMapper.selectByType(vipcard.getVipcardType());
        if (vipcard1==null){
            Integer add = vipcardMapper.add(vipcard);
            return add;
        }else{
            throw new BusinessException(ExceptionCode.DUPLICATE_VIPCARD);
        }
    }

    @Override
    public Integer delete(Long vipcardId) throws BusinessException {
        Vipcard vipcard = vipcardMapper.selectById(vipcardId);
        if (vipcard!=null){
            Integer delete = vipcardMapper.delete(vipcardId);
            return delete;
        }else{
            throw new BusinessException(ExceptionCode.NO_VIPCARD);
        }
    }

    @Override
    public void update(Vipcard vipcard) throws BusinessException {
        Vipcard vipcard1 = vipcardMapper.selectById(vipcard.getVipcardId());
        if (vipcard1!=null){
            vipcardMapper.update(vipcard);
        }else{
            throw new BusinessException(ExceptionCode.NO_VIPCARD);
        }

    }
}
